package com.ldp.designpatterns.study.iterator.iterator;

import java.util.List;

/**
 * @ClassName ConcreteIterator TODO:第2步：具体迭代器实现迭代器接口 ,重写hasNext和next方法
 * @Description TODO:迭代器模式 迭代器模式提供了一种统一的方法来遍历不同类型的集合
 * @Author lindipei
 * @Date 2023/10/18 11:03
 */
public class ConcreteIteratorD<T> implements IteratorD<T> {

    private List<T> items;
    private int position = 0;

    public ConcreteIteratorD(List<T> items) {
        this.items = items;
    }

    @Override
    public boolean hasNext() {
        return position < items.size();
    }

    @Override
    public T next() {
        if (hasNext()) {
            T item = items.get(position);
            position++;
            return item;
        }
        throw new IndexOutOfBoundsException("No more elements");
    }
}
